/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.figures;


import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.figures.LabelFigure;

/**
 * To diplay the operation.
 * 
 * @author Patrick Tessier
 */
public class OperationFigure extends LabelFigure {
	
	/**
	 * 
	 */
	protected static String _public=" +";
	
	/**
	 * 
	 */
	protected static String _protected=" #";
	
	/**
	 * 
	 */
	protected static String _private=" -";
	
	
	/**
	 * 
	 */
	protected Label label=new Label();
	
	/**
	 * 
	 */
	protected String visibility=_public;
	
	/**
	 * 
	 */
	protected String name="";
	
	/**
	 * 
	 */
	protected String type="";
	
	/**
	 * 
	 */
	protected String stereotypes="";
	
	/**
	 * 
	 */
	protected String text="";
	
	/**
	 * 
	 */
	protected boolean isStatic=false;
	
	/**
	 * 
	 */
	protected ArrayList<String> parameters=new ArrayList<String>();
	
	/**
	 * 
	 */
	public OperationFigure()
	{
		FontData[] font={new FontData("Arial", 9, SWT.NORMAL)};
		setFont(PapyrusPlugin.fontManager.get(font));
		setLabelAlignment(PositionConstants.LEFT );
	}
	
	/**
	 * 
	 * 
	 * @param text 
	 * @param isAbstract 
	 */
	public void display(String text, boolean isAbstract) {
		setText(stereotypes+text);
		setAbstract(isAbstract);
	}

	/**
	 * to add a name.
	 * 
	 * @param name of the operation
	 */
	@Override
	public void setName(String name){
		this.name=name;
		setText(visibility+" "+name);
		display();
	}
	
	/**
	 * to add stereotypes.
	 * 
	 * @param name of the operation
	 * @param st 
	 */
	@Override
	public void setStereotypes(String st){
		if (st==null) {
			this.stereotypes="";
		} else {
			this.stereotypes=PapyrusPlugin.ST_LEFT+st+PapyrusPlugin.ST_RIGHT;
		}
		display();
	}

	/**
	 * to add a type of the operation.
	 * 
	 * @param type 
	 */
	public void setType(String type){
		if (type=="") {
			this.type="";
		} else {
			this.type=": "+type;
		}
		display();
	}
	
	
	/**
	 * display the operation figure.
	 */
	protected void display(){
		String params="";
		//list all parameters
		Iterator<String> it =parameters.iterator();
		while (it.hasNext()){
			params= params+(String)it.next();
			if (it.hasNext()) {
				params=params+", ";
			}
		}
		setText(stereotypes+visibility+" "+name+"("+params+")"+type);
	}
	
	/**
	 * delete all parameters.
	 */
	public void clearParameter(){
		this.parameters=new ArrayList<String>();
		this.display();
	}
	
	/**
	 * add one paremeter.
	 * 
	 * @param direction of the parameter
	 * @param type of the parameter
	 * @param name of the parameter
	 */
	public void addParameter(String direction, String name, String type){
		this.parameters.add(direction+" "+name+": "+type);
		this.display();
	}
	
	/**
	 * display the operation as private.
	 */
	public void setPrivate(){
		this.visibility=_private;
		display();
	}
	
	/**
	 * display the operation as protected.
	 */
	public void setProtected(){
		this.visibility=_protected;
		display();
	}
	
	/**
	 * display the operation as public.
	 */
	public void setPublic(){
		this.visibility=_public;
		display();
	}
	
	/**
	 * display the operation as abstract.
	 * 
	 * @param b true for abstract; false other
	 */
	public void setAbstract(boolean b){
		
		if (b){
			FontData[] font={new FontData(fontString, fontSize, SWT.ITALIC)};
			this.setFont(PapyrusPlugin.fontManager.get(font));
			}
		else{
			FontData[] font={new FontData(fontString, fontSize, SWT.NORMAL)};
			this.setFont(PapyrusPlugin.fontManager.get(font));
			}
	}
	
	/**
	 * display the element as static.
	 * 
	 * @param b 
	 */
	public void setStatic(boolean b){
		this.isStatic=b;
	}
	
	/**
	 * paint the label.
	 * 
	 * @param graphics 
	 */
	@Override
	public void paint (Graphics graphics) {
		super.paint (graphics);
		Rectangle rect;
		rect = this.getBounds();
		if (isStatic){
			graphics.setBackgroundColor(ColorConstants.black);
			Dimension labelSize=calculateTextSize();
			graphics.drawLine(rect.x+2, rect.y+rect.height-2, rect.x+labelSize.width, rect.y+rect.height-2); //separator between compartement
		}
	}
}
